package com.zf.shuashua.ui.popup;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aliyun.common.utils.DensityUtil;
import com.meis.base.mei.base.BaseFragment;
import com.zf.shuashua.R;
import com.zf.shuashua.manager.AdAutoTouchManager;

public class RewardPopupFragment extends BaseFragment {

    public static final String TITLE = "title";
    public static final String SUB_TITLE = "subTitle";
    public static final String VALUE = "value";
    public static final String UI_TYPE = "uiType";
    public static final String TYPE = "type";

    private TextView tvSum, tvTitle, btnTitle, tvUnit, tvbTitle, tvHint, tvHint2, tvNum2, tvUnit2;
    private ImageView imgClose, imgPlay, imgShapeBg;
    private FrameLayout advert_container;
    private String title, subTitle, value, uiType, type;
    private OnPopupListener mListener;
    private LinearLayout incomeLayout;
    private View view;

    public static RewardPopupFragment newInstance(String title, String subTitle, String value, String uiType, String type) {
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putString(SUB_TITLE, subTitle);
        args.putString(VALUE, value);
        args.putString(UI_TYPE, uiType);
        args.putString(TYPE, type);
        RewardPopupFragment fragment = new RewardPopupFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            title = getArguments().getString(TITLE, "");
            subTitle = getArguments().getString(SUB_TITLE, "");
            value = getArguments().getString(VALUE, "");
            uiType = getArguments().getString(UI_TYPE, "");
            type = getArguments().getString(TYPE, "");
        }

        tvHint = findViewById(R.id.tvHint);
        tvHint2 = findViewById(R.id.tvHint2);
        tvNum2 = findViewById(R.id.tvNum2);
        tvUnit2 = findViewById(R.id.tvUnit2);
        imgShapeBg = findViewById(R.id.img_backdrop);
        tvTitle = findViewById(R.id.tvTitle);
        tvSum = findViewById(R.id.tvSum);
        incomeLayout = findViewById(R.id.layoutIncome);
        btnTitle = findViewById(R.id.btTitle);
        tvUnit = findViewById(R.id.tvUnit);
        imgClose = findViewById(R.id.img_close);
        imgPlay = findViewById(R.id.iv_play_icon);
        tvbTitle = findViewById(R.id.tvbTitle);
        advert_container = findViewById(R.id.advert_container);
        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                if (mListener != null) {
                    mListener.onNext();
                }
            }
        });


//        if (Store.getInstance().getTP_AD()) {
//            findViewById(R.id.flCancel).setOnClickListener(v -> {
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//        } else {
//            btnTitle.setOnClickListener(v -> {
//                if (mListener != null) {
//                    mListener.onNext();
//                }
//            });
//        }

        imgClose.setOnClickListener(v -> {
//            dismiss();
            if (mListener != null) {
                mListener.onClose();
            }
        });
    }

    @Override
    protected void initData() {
        String unit = "新客专享礼包".equals(title) || "领取成功!".equals(title) ? "元" : "金豆";

        tvUnit.setText(unit);
        tvTitle.setText(title);
        tvSum.setText(value);
        if ("0".equals(uiType)) {
//            btnTitle.setVisibility(VISIBLE);
            btnTitle.setText(subTitle);
        } else {
            tvbTitle.setVisibility(View.VISIBLE);
            tvbTitle.setText(subTitle);
        }

        if (!TextUtils.isEmpty(type)) {
            if (type.equals("PUNCH") || type.equals("TIME_LIMIT")) {
                incomeLayout.setVisibility(View.VISIBLE);
                imgShapeBg.getLayoutParams().height = DensityUtil.dip2px(getActivity(), 180);
                imgShapeBg.requestLayout();
            }
            if (type.equals("TIME_LIMIT")) {
                tvHint.setText("此次登录获得");
                tvHint2.setText("累计登录10次可赚");
                tvNum2.setText("588");
                tvUnit2.setText("金豆");
            }
        } else {
            incomeLayout.setVisibility(View.GONE);
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.reward_popup;
    }

    public void addAdvertView(View view) {
        this.view = view;
        if(PopupUtil.isPopupEnd(uiType, subTitle)){
            imgClose.setVisibility(View.VISIBLE);
        }
        advert_container.removeAllViews();
        advert_container.addView(view);
        if ("0".equals(uiType)) {
            btnTitle.setVisibility(View.VISIBLE);
        }
    }

    public void loadAdvertFail() {
        if(PopupUtil.isPopupEnd(uiType, subTitle)){
            imgClose.setVisibility(View.VISIBLE);
        }
        if ("0".equals(uiType)) {
            btnTitle.setVisibility(View.VISIBLE);
        }
        advert_container.setVisibility(View.GONE);
    }
}
